package com.lottery.material.init;

import com.lottery.material.dao.report.ReportTypeDictMapper;
import com.lottery.material.jedis.JedisClient;
import com.lottery.material.pojo.report.ReportTypeDict;
import com.lottery.material.util.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Description：项目启动时，举报类型字典数据添加到Redis进行缓存
 * Created by 朱正磊 on 2019-04-18 15:31
 */
@Slf4j
@Component
@SuppressWarnings("SpringAutowiredFieldsWarningInspection")
public class ReportTypeDictInit implements InitializingBean {

    @Autowired
    @Qualifier("jedisClientCluster")
    private JedisClient jedisClient;

    @Autowired
    private ReportTypeDictMapper reportTypeDictMapper;

    @Override
    public void afterPropertiesSet() { // 在spring初始化该bean的时候会调用此方法
        List<ReportTypeDict> list = reportTypeDictMapper.findAll();
        try {
            list.forEach(e -> jedisClient.hset("reportTypeDict", e.getReportCode(), e.getReportName()));
        } catch (Exception e) {
            log.error("【举报类型字典数据添加到Redis进行缓存异常：{}】", ExceptionUtil.logExceptionStack(e));
        }
    }

}
